<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="水管员名称" prop="mainNm">
        <el-input
          v-model="queryParams.mainNm"
          placeholder="请输入水管员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属机构" prop="orgCd">
        <treeselect
          v-model="queryParams.orgCd"
          :options="xzqhOptions"
          :normalizer="normalizer"
          :show-count="true"
          placeholder="水管机构"
          @select="selectAd"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datam:WiMainB:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['datam:WiMainB:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['datam:WiMainB:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['datam:WiMainB:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="WiMainBList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="水管员编码" align="center" prop="mainCd" /> -->
      <el-table-column
        label="水管员名称"
        align="left"
        prop="mainNm"
        min-width="120"
      >
      </el-table-column>
      <el-table-column
        label="所属机构"
        align="left"
        prop="orgNm"
        min-width="180"
      >
      </el-table-column>
      <!-- <el-table-column label="水管员地址" align="center" prop="mobileAddr" min-width="180">
      </el-table-column> -->
      <el-table-column
        label="水管员电话"
        align="center"
        prop="mobileNo"
        min-width="100"
      >
      </el-table-column>
      <el-table-column label="当前状态" align="center" prop="mainStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.main_status"
            :value="scope.row.mainStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="聘用日期"
        align="center"
        prop="beginDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="解聘日期"
        align="center"
        prop="endDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作人员" align="center" prop="mainOper">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="mainMark">
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        min-width="120"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['datam:WiMainB:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datam:WiMainB:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水管员信息登记对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :inline="true"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="水管员名称" prop="mainNm">
              <el-input v-model="form.mainNm" placeholder="请输入水管员名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="水管员地址" prop="mobileAddr">
              <el-input
                v-model="form.mobileAddr"
                placeholder="请输入水管员地址"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="水管员电话" prop="mobileNo">
              <el-input
                v-model="form.mobileNo"
                placeholder="请输入水管员电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属机构" prop="orgCd">
              <treeselect
                v-model="form.orgCd"
                :options="xzqhOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="水管机构"
                @select="selectAd"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="聘用日期" prop="beginDate">
              <el-date-picker
                clearable
                v-model="form.beginDate"
                type="date"
                class="w202"
                value-format="yyyy-MM-dd"
                placeholder="请选择聘用日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="解聘日期" prop="endDate">
              <el-date-picker
                clearable
                v-model="form.endDate"
                type="date"
                class="w202"
                value-format="yyyy-MM-dd"
                placeholder="请选择解聘日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="当前状态" prop="mainStatus">
              <el-select
                class="w202"
                v-model="form.mainStatus"
                placeholder="请选择水管员状态"
              >
                <el-option
                  v-for="dict in dict.type.main_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="mainMark">
              <el-input v-model="form.mainMark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="24">
            <el-form-item label="管辖渠系" prop="chanCd">
              <treeselect
                :multiple="true"
                v-model="form.chanCd"
                :options="xzqhOptions2"
                :normalizer="normalizer2"
                :show-count="true"
                search-nested
                placeholder="管辖渠系"
                @select="selectAd"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWiMainB,
  getWiMainB,
  delWiMainB,
  addWiMainB,
  updateWiMainB,
} from "@/api/datam/WiMainB";
import { organtreeList, WcChanBList } from "@/api/common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  name: "WiMainB",
  dicts: ["main_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水管员信息登记表格数据
      WiMainBList: [],
      xzqhOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mainNm: null,
        mobileAddr: null,
        mobileNo: null,
        orgCd: null,
        mainStatus: null,
        beginDate: null,
        endDate: null,
        mainOper: null,
        mainMark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mainNm : [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        mobileNo: [
          {
            required: true,
            type: 'number',
            message: '手机号格式不正确',
            trigger: 'blur',
            transform(value) {
              var phonereg = 11 && /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
              if (!phonereg.test(value)) {
                return false
              } else {
                return Number(value)
              }
            }
          }
        ],
      },
    };
  },
  created() {
    this.getTreeselect();
    this.getTreeselect2();
    this.getList();
  },
  methods: {
    /** 查询渠系下拉树结构 */
    getTreeselect2() {
      WcChanBList().then((response) => {
        this.xzqhOptions2 = this.handleTree(
          response.data,
          "chanCd",
          "parentChanCd"
        );
      });
    },
    /** 查询行政区域下拉树结构 */
    getTreeselect() {
      organtreeList().then((response) => {
        this.xzqhOptions = this.handleTree(
          response.data,
          "orgCd",
          "parentOrgCd"
        );
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgCd,
        label: node.orgNm,
        children: node.children,
      };
    },
    /** 转换菜单数据结构 */
    normalizer2(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.chanCd,
        label: node.chanNm,
        children: node.children,
      };
    },
    // 获取区划名称
    selectAd(val) {
      // this.queryParams.tableTitle = val.orgNm
      // this.getlistWiFarmer()
    },
    /** 查询水管员信息登记列表 */
    getList() {
      this.loading = true;
      listWiMainB(this.queryParams).then((response) => {
        this.WiMainBList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mainCd: null,
        mainNm: null,
        mobileAddr: null,
        mobileNo: null,
        orgCd: null,
        mainStatus: null,
        beginDate: null,
        endDate: null,
        mainOper: null,
        mainMark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.mainCd);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加水管员信息登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mainCd = row.mainCd || this.ids;
      getWiMainB(mainCd).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改水管员信息登记";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.mainCd != null) {
            updateWiMainB(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWiMainB(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mainCds = row.mainCd || this.ids;
      this.$modal
        .confirm('是否确认删除水管员信息登记编号为"' + mainCds + '"的数据项？')
        .then(function () {
          return delWiMainB(mainCds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "datam/WiMainB/export",
        {
          ...this.queryParams,
        },
        `水管员管理_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.w202 {
  width: 202px;
}
</style>


